package com.ecy.quadratic.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ListView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ComicAdapter;
import com.ecy.quadratic.beans.Comic;
import com.ecy.quadratic.beans.ComicResponseBean;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.request.BasicRequest.OnResponseListener;
import com.ecy.quadratic.request.GirlComicRequest;
import com.ecy.quadratic.widget.SwipeRefreshView;
import com.ecy.quadratic.widget.SwipeRefreshView.OnLoadListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Administrator on 2016/3/31.
 */
public class GirlComicFragment extends Fragment {
    private SwipeRefreshView mSwipeRefreshView;
    private ListView mComicListView;
    private View mRootView;
    private ComicAdapter mAdapter;
    private List<Comic> mComicList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;
    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            getComicsForRefresh(1);
        }
    };
    private OnLoadListener mOnLoadListener = new OnLoadListener() {

        @Override
        public void onLoad() {
            getComicsForLoad(mCurrentPage + 1);
        }
    };

    public GirlComicFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initView(inflater, container);
    }

    private View initView(LayoutInflater inflater, ViewGroup container) {
        if (mRootView != null) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
            return mRootView;
        }
        mRootView = inflater.inflate(R.layout.activity_comic_list, null);
        mSwipeRefreshView = (SwipeRefreshView) mRootView.findViewById(R.id.comit_swipe_refreshview);
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.setOnLoadListener(mOnLoadListener);
        mSwipeRefreshView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                mSwipeRefreshView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
        mComicListView = (ListView) mRootView.findViewById(R.id.comit_listview);
        mComicList = new ArrayList<Comic>();
        mAdapter = new ComicAdapter(getContext(), mComicList);
        mComicListView.setAdapter(mAdapter);
        return mRootView;
    }

    private void getComicsForRefresh(int page) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("page", page + "");
        new GirlComicRequest(hashMap).execute(new OnResponseListener<ComicResponseBean>() {

            @Override
            public void onSuccess(ComicResponseBean t,
                                  HashMap<String, String> requestParams) {
                if (t != null) {
                    PageInfo<Comic> pageInfo = t.getResult();
                    if (pageInfo != null && mTotalPage != pageInfo.pages) {
                        mCurrentPage = pageInfo.pageNum;
                        mTotalPage = pageInfo.pages;
                        mComicList.addAll(pageInfo.list);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                mSwipeRefreshView.setRefreshing(false);
            }

            @Override
            public void onFailed(HashMap<String, String> requestParams) {
                mSwipeRefreshView.setRefreshing(false);
            }
        });
    }

    private void getComicsForLoad(int page) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("page", page + "");
        new GirlComicRequest(hashMap).execute(new OnResponseListener<ComicResponseBean>() {

            @Override
            public void onSuccess(ComicResponseBean t,
                                  HashMap<String, String> requestParams) {
                mSwipeRefreshView.setLoading(false);
                if (t != null) {
                    PageInfo<Comic> pageInfo = t.getResult();
                    if (pageInfo != null) {
                        mCurrentPage = pageInfo.pageNum;
                        mTotalPage = pageInfo.pages;
                        mComicList.addAll(pageInfo.list);
                        mAdapter.notifyDataSetChanged();
                    }
                }

            }

            @Override
            public void onFailed(HashMap<String, String> requestParams) {
                mSwipeRefreshView.setLoading(false, true);
            }
        });
    }
}
